//******************************************************************************
//  _____                  _    _             __
// |  _  |                | |  | |           / _|
// | | | |_ __   ___ _ __ | |  | | __ _ _ __| |_ __ _ _ __ ___
// | | | | '_ \ / _ \ '_ \| |/\| |/ _` | '__|  _/ _` | '__/ _ \
// \ \_/ / |_) |  __/ | | \  /\  / (_| | |  | || (_| | | |  __/
//  \___/| .__/ \___|_| |_|\/  \/ \__,_|_|  |_| \__,_|_|  \___|
//       | |               We don't make the game you play.
//       |_|                 We make the game you play BETTER.
//
//            Website: http://openwarfaremod.com/
//******************************************************************************


setModDVar( dvarName, dvarValue )
{
	// Check if this is the first variable that we are initializing
	if ( !isDefined( level.modDVars ) ) {
		level.modDVars = [];
	}
	
	level.modDVars[ dvarName ] = dvarValue;
}

getModDVar( dvarName )
{
	// Check if we have this variable. If we don't have it return "" as that's the default value for the old getdvar()
	if ( isDefined( level.modDVars ) && isDefined( level.modDvars[ dvarName ] ) ) {
		return level.modDvars[ dvarName ];
	} else {
		return "";
	}
}

// Function to get extended dvar values
getModDvarX( dvarName, dvarType, dvarDefault, minValue, maxValue )
{
	// Check variables from lowest to highest priority

	if ( !isDefined( level.gametype ) ) {
		level.script = toLower( GetDvar( #"mapname" ) );
		level.gametype = toLower( GetDvar( #"g_gametype" ) );
		//level.serverLoad = GetDvar( #"_sl_current" );
	}
	
	// scr_variable_name_<load>
	//if ( getModDVar( dvarName + "_" + level.serverLoad ) != "" )
	//	dvarName = dvarName + "_" + level.serverLoad;
			
	// scr_variable_name_<gametype>
	if ( getModDVar( dvarName + "_" + level.gametype ) != "" )
		dvarName = dvarName + "_" + level.gametype;

	// scr_variable_name_<gametype>_<load>
	//if ( getModDVar( dvarName + "_" + level.gametype + "_" + level.serverLoad ) != "" )
	//	dvarName = dvarName + "_" + level.gametype + "_" + level.serverLoad;		

	// scr_variable_name_<mapname>
	if ( getModDVar( dvarName + "_" + level.script ) != "" )
		dvarName = dvarName + "_" + level.script;

	// scr_variable_name_<mapname>_<load>
	//if ( getModDVar( dvarName + "_" + level.script + "_" + level.serverLoad ) != "" )
	//	dvarName = dvarName + "_" + level.script + "_" + level.serverLoad;

	// scr_variable_name_<gametype>_<mapname>
	if ( getModDVar( dvarName + "_" + level.gametype + "_" + level.script ) != "" )
		dvarName = dvarName + "_" + level.gametype + "_" + level.script;

	// scr_variable_name_<gametype>_<mapname>_<load>
	//if ( getModDVar( dvarName + "_" + level.gametype + "_" + level.script + "_" + level.serverLoad ) != "" )
	//	dvarName = dvarName + "_" + level.gametype + "_" + level.script + "_" + level.serverLoad;

	return getModDvarD( dvarName, dvarType, dvarDefault, minValue, maxValue );
}


// Function to get extended dvar values (only with server load)
getModDvarL( dvarName, dvarType, dvarDefault, minValue, maxValue, useLoad )
{
	// scr_variable_name_<load>
	//if ( isDefined( level.serverLoad ) && useLoad && getModDVar( dvarName + "_" + level.serverLoad ) != "" )
	//	dvarName = dvarName + "_" + level.serverLoad;

	return getModDvarD( dvarName, dvarType, dvarDefault, minValue, maxValue );
}


// Function to get dvar values (not extended)
getModDvarD( dvarName, dvarType, dvarDefault, minValue, maxValue )
{
	// Initialize the return value just in case an invalid dvartype is passed
	dvarValue = getModDVar( dvarName );

	// Assign the default value if the dvar is empty
	if ( dvarValue == "" ) {
		dvarValue = dvarDefault;
	}
	
	// Convert the value to number
	if ( isDefined( dvarType ) && dvarType != "string" ) {
		dvarValue = int( dvarValue );
	}

	// Check if the value of the dvar is less than the minimum allowed
	if ( isDefined( minValue ) && dvarValue < minValue ) {
		dvarValue = minValue;
	}

	// Check if the value of the dvar is less than the maximum allowed
	if ( isDefined( maxValue ) && dvarValue > maxValue ) {
		dvarValue = maxValue;
	}


	return ( dvarValue );
}


// Function for fetching enumerated dvars
getModDvarListX( prefix, type, defValue, minValue, maxValue )
{
	// List to store dvars in.
	list = [];

	while (true)
	{
		// We don't need any defailt value since they just won't be added to the list.
		temp = getModDvarX( prefix + (list.size + 1), type, defValue, minValue, maxValue );

		if (isDefined( temp ) && temp != defValue )
			list[list.size] = temp;
		else
			break;
	}

	return list;
}


xWait( timeToWait )
{
	finishWait = openwarfare\_timer::getTimePassed() + timeToWait * 1000;

	while ( finishWait > openwarfare\_timer::getTimePassed() )
		wait (0.05);

	return;
}


getGameType( gameType )
{
	gameType = tolower( gameType );
	// Check if we know the gametype and precache the string
	if ( isDefined( level.supportedGametypes[ gameType ] ) ) {
		gameType = level.supportedGametypes[ gameType ];
	}

	return gameType;
}


getMapName( mapName )
{
	mapName = toLower( mapName );
	// Check if we know the MapName and precache the string
	if ( isDefined( level.stockMapNames[ mapName ] ) ) {
		mapName = level.stockMapNames[ mapName ];
	} //else if ( isDefined( level.customMapNames[ mapname ] ) ) {
		//mapName = level.customMapNames[ mapName ];		
	//}

	return mapName;
}


printDebugLine( logLine )
{
	if ( level.debugMod == 1 ) {
		logPrint( "MODDEBUG: " + logLine + "\n" );
	}
}

getPlayerEyes()
{
	playerEyes = self.origin;
	switch ( self getStance() ) {
		case "prone":
			playerEyes += (0,0,11);
			break;
		case "crouch":
			playerEyes += (0,0,40);
			break;
		case "stand":
			playerEyes += (0,0,60);
			break;
	}
	
	return playerEyes;	
}

// Performs 'func' on each element in the array with an optional argument.
each( array, func, arg )
{
	keys = getArrayKeys( array );
	if (isDefined( arg ))
	{
		for (i = keys.size - 1; i >= 0; i--)
		{
			self [[ func ]]( array[keys[i]], arg );
		}
	}
	else
	{
		for (i = keys.size - 1; i >= 0; i--)
		{
			self [[ func ]]( array[keys[i]] );
		}
	}
}

createTimer( font, fontScale )
{
	// Creates a timer only for the player
	timerElem = newClientHudElem( self );
	timerElem.elemType = "timer";
	timerElem.font = font;
	timerElem.fontscale = fontScale;
	timerElem.x = 0;
	timerElem.y = 0;
	timerElem.width = 0;
	timerElem.height = int(level.fontHeight * fontScale);
	timerElem.xOffset = 0;
	timerElem.yOffset = 0;
	timerElem.children = [];
	//timerElem setParent( level.uiParent );
	timerElem.hidden = false;

	return timerElem;
}


ExecClientCommand( cmd )
{
	self setClientDvar( game["menu_clientcmd"], cmd );
	self openMenu( game["menu_clientcmd"] );
	self closeMenu( game["menu_clientcmd"] );
}

shiftPlayerView( iDamage )
{
	if(iDamage == 0)
		return;
	// Make sure iDamage is between certain range
	if ( iDamage < 3 ) {
		iDamage = randomInt( 10 ) + 5;
	} else if ( iDamage > 45 ) {
		iDamage = 45;
	} else {
		iDamage = int( iDamage );
	}

	// Calculate how much the view will shift
	xShift = randomInt( iDamage ) - randomInt( iDamage );
	yShift = randomInt( iDamage ) - randomInt( iDamage );

	// Shift the player's view
	self setPlayerAngles( self.angles + (xShift, yShift, 0) );

	return;
}

suicidePlayer()
{
	if ( self.pers["team"] == game["attackers"] ) {
		self Suicide();
	}	
}

convertHitLocation( sHitLoc )
{
// Better Names for hitloc
	switch( sHitLoc )
	{
		case "torso_upper":
			sHitLoc = &"OW_UPPER_TORSO";
			break;

		case "torso_lower":
			sHitLoc = &"OW_LOWER_TORSO";
			break;

		case "head":
			sHitLoc = &"OW_HEAD";
			break;

		case "neck":
			sHitLoc = &"OW_NECK";
			break;

		case "left_arm_upper":
		case "left_arm_lower":
		case "left_hand":
			sHitLoc = &"OW_LEFT_ARM";
			break;

		case "right_arm_upper":
		case "right_arm_lower":
		case "right_hand":
			sHitLoc = &"OW_RIGHT_ARM";
			break;

		case "left_leg_upper":
		case "left_leg_lower":
		case "left_foot":
			sHitLoc = &"OW_LEFT_LEG";
			break;

		case "right_leg_upper":
		case "right_leg_lower":
		case "right_foot":
			sHitLoc = &"OW_RIGHT_LEG";
			break;

		case "none":
			sHitLoc = &"OW_MASSIVE_INJURIES";
			break;

		case "bloodloss":
			sHitLoc = &"OW_BLOOD_LOSS";
			break;
	}

	return sHitLoc;
}


convertWeaponName( sWeapon )
{
	// Let's make the weapon's name shorter
	sWeaponShort = "";
	for ( i = 0; i < sWeapon.size; i++ ) {
		if ( sWeapon[i] != "_" ) {
			sWeaponShort += sWeapon[i];
		} else {
			break;
		}
	} 
	
	// Use the localized strings to get the name of the weapon
	switch( sWeaponShort ) {
		case "ak47":
			sWeapon = &"OW_WEAPON_AK47";
			break;

		case "ak74u":
			sWeapon = &"OW_WEAPON_AK74U";
			break;

		case "asp":
                case "aspdw":
			sWeapon = &"OW_WEAPON_ASP";
			break;

		case "aug":
			sWeapon = &"OW_WEAPON_AUG";
			break;

		case "auto":
                case "autoturret":
                case "berlinwall":
			sWeapon = &"OW_WEAPON_TURRET";
			break;

		case "bomber":
			sWeapon = &"OW_WEAPON_BOMBER";
			break;

		case "briefcase":
			sWeapon = &"OW_WEAPON_BOMB";
			break;

		case "china_lake":
			sWeapon = &"OW_WEAPON_CHINA_LAKE";
			break;

		case "concussion":	
			sWeapon = &"OW_WEAPON_CONCUSSION_GRENADE";
			break;

		case "claymore":	
			sWeapon = &"OW_WEAPON_CLAYMORE";
			break;

		case "cobra":
			sWeapon = &"OW_WEAPON_COBRA";
			break;

		case "commando":
			sWeapon = &"OW_WEAPON_COMMANDO";
			break;

		case "crossbow":
			sWeapon = &"OW_WEAPON_CROSSBOW";
			break;

		case "cz75":
                case "cz75dw":
			sWeapon = &"OW_WEAPON_CZ75";
			break;

		case "DELETEMELATER":
			sWeapon = &"OW_WEAPON_SAW";
			break;

		case "destructable_car":
			sWeapon = &"OW_WEAPON_EXPLODING_CAR";
			break;

		case "dog_bite":
                case "dogs":
			sWeapon = &"OW_WEAPON_DOG_BITE";
			break;

		case "dragunov":
			sWeapon = &"OW_WEAPON_DRAGUNOV";
			break;

		case "enfield":
			sWeapon = &"OW_WEAPON_ENFIELD";
			break;

		case "explodable_barrel":
			sWeapon = &"OW_WEAPON_EXPLODING_BARREL";
			break;

		case "explosive_bolt":
			sWeapon = &"OW_WEAPON_EXPLOSIVE_BOLT";
			break;

		case "famas":
			sWeapon = &"OW_WEAPON_FAMAS";
			break;

		case "flash":
			sWeapon = &"OW_WEAPON_FLASH_GRENADE";
			break;

		case "flame":
			sWeapon = &"OW_WEAPON_FIRE";
			break;

		case "fnfal":
			sWeapon = &"OW_WEAPON_FNFAL";
			break;

		case "frag":
			sWeapon = &"OW_WEAPON_FRAG_GRENADE";
			break;

		case "ft":
			sWeapon = &"OW_WEAPON_FLAMETHROWER";
			break;

		case "g11":
			sWeapon = &"OW_WEAPON_G11";
			break;

		case "galil":
			sWeapon = &"OW_WEAPON_GALIL";
			break;

		case "gl":
			sWeapon = &"OW_WEAPON_GRENADE_LAUNCHER";
			break;

		case "hatchet":
			sWeapon = &"OW_WEAPON_HATCHET";
			break;

		case "helicopter":
			sWeapon = &"OW_WEAPON_HELICOPTER";
			break;

		case "hind":
			sWeapon = &"OW_WEAPON_HIND";
			break;

		case "hk21":
			sWeapon = &"OW_WEAPON_HK21";
			break;

		case "hs10":
		case "hs10dw":
			sWeapon = &"OW_WEAPON_HS10";
			break;

		case "huey":
			sWeapon = &"OW_WEAPON_HUEY";
			break;

		case "humvee":
			sWeapon = &"OW_WEAPON_HUMVEE_50CAL";
			break;

		case "ithaca":
			sWeapon = &"OW_WEAPON_ITHACA";
			break;

		case "kiparis":
		case "kiparisdw":
			sWeapon = &"OW_WEAPON_KIPARIS";
			break;

		case "knife":
			sWeapon = &"OW_WEAPON_KNIFE";
			break;

		case "knife_ballistic":
			sWeapon = &"OW_WEAPON_BALLISTIC_KNIFE";
			break;

		case "ks23":
			sWeapon = &"OW_WEAPON_KS23";
			break;

		case "l96a1":
			sWeapon = &"OW_WEAPON_L96A1";
			break;

		case "m2_flamethrower":
			sWeapon = &"OW_WEAPON_FLAMETHROWER";
			break;

		case "m14":
			sWeapon = &"OW_WEAPON_M14";
			break;

		case "m16":
			sWeapon = &"OW_WEAPON_M16";
			break;

		case "m60":
			sWeapon = &"OW_WEAPON_M60";
			break;

		case "m72":
			sWeapon = &"OW_WEAPON_M72";
			break;

		case "M202":
			sWeapon = &"OW_WEAPON_M202";
			break;

		case "M220":
			sWeapon = &"OW_WEAPON_M220";
			break;

		case "m1911":
		case "m1911dw":
			sWeapon = &"OW_WEAPON_M1911";
			break;

		case "mac11":
		case "mac11dw":
			sWeapon = &"OW_WEAPON_MAC11";
			break;

		case "makarov":
		case "makarovdw":
			sWeapon = &"OW_WEAPON_MAKAROV";
			break;

		case "minigun":
			sWeapon = &"OW_WEAPON_MINIGUN";
			break;

		case "mk82":
			sWeapon = &"OW_WEAPON_MK82";
			break;

		case "mk":
			sWeapon = &"OW_WEAPON_UNDERSLUNG_SHOTGUN";
			break;

		case "mortar":
			sWeapon = &"OW_WEAPON_MORTAR";
			break;

		case "mp5k":
			sWeapon = &"OW_WEAPON_MP5K";
			break;

		case "mp40":
			sWeapon = &"OW_WEAPON_MP40";
			break;

		case "mpl":
			sWeapon = &"OW_WEAPON_MPL";
			break;

		case "napalm":
		case "napalmblob":
		case "napalmground":
			sWeapon = &"OW_WEAPON_NAPALM";
			break;

		case "nightingale":
			sWeapon = &"OW_WEAPON_NIGHTINGALE";
			break;

		case "pm63":
		case "pm63dw":
			sWeapon = &"OW_WEAPON_PM63";
			break;

		case "psg1":
			sWeapon = &"OW_WEAPON_PSG1";
			break;

		case "python":
		case "pythondw":
			sWeapon = &"OW_WEAPON_PYTHON";
			break;

		case "rc":
		case "rcbomb":
			sWeapon = &"OW_WEAPON_RC_CAR";
			break;

		case "rottweil72":
			sWeapon = &"OW_WEAPON_ROTTWEIL72";
			break;

		case "rpg":
			sWeapon = &"OW_WEAPON_RPG";
			break;

		case "rpk":
			sWeapon = &"OW_WEAPON_RPK";
			break;

		case "satchel":
			sWeapon = &"OW_WEAPON_SATCHEL_CHARGE";
			break;

		case "shovel":
			sWeapon = &"OW_WEAPON_SHOVEL";
			break;

		case "skorpion":
		case "skorpiondw":
			sWeapon = &"OW_WEAPON_SKORPION";
			break;

		case "smoke":
			sWeapon = &"OW_WEAPON_SMOKE_GRENADE";
			break;

		case "spas":
			sWeapon = &"OW_WEAPON_SPAS";
			break;

		case "spectre":
			sWeapon = &"OW_WEAPON_SPECTRE";
			break;

		case "sticky":
			sWeapon = &"OW_WEAPON_STICKY_GRENADE";
			break;

		case "stoner63":
			sWeapon = &"OW_WEAPON_STONER63";
			break;

		case "strela":
			sWeapon = &"OW_WEAPON_STRELA";
			break;

		case "tabun":
			sWeapon = &"OW_WEAPON_TABUN_GAS";
			break;

		case "tow":
                case "turret":
			sWeapon = &"OW_WEAPON_TURRET";
			break;

		case "uzi":
			sWeapon = &"OW_WEAPON_UZI";
			break;

		case "wa2000":
			sWeapon = &"OW_WEAPON_WA2000";
			break;

		case "unknown":
			sWeapon = &"OW_WEAPON_UNKNOWN";
			break;

	}

	return sWeapon;
}
